package com.njtoyo.taxi.admin.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.manager.AsyncManager;
import com.njtoyo.taxi.admin.manager.factory.AsyncFactory;
import com.njtoyo.taxi.admin.mapper.master.entity.CallEndSend;
import com.njtoyo.taxi.admin.mapper.master.mapper.CallEndSendMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 转存东信话单的录音到云
 *
 * @author qiuping
 * @since 2024/7/12
 */
@Deprecated
@Component
@Slf4j
public class CallRelevanceFinishRecordUrlToOSSTask {
    @Autowired
    private CallEndSendMapper callEndSendMapper;

    /**
     * 每5分钟执行一次
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void query() {
        // 结束时间
        Date endTime =new Date();
        // 开始时间
        Date startTime =subtractMinutes(endTime,-5);
        // 创建QueryWrapper
        QueryWrapper<CallEndSend> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("creatBy_time", startTime, endTime);
        queryWrapper.isNull("record_url_oss");
        List<CallEndSend> callEndSends = callEndSendMapper.selectList(queryWrapper);
        log.info("callEndSends:{}",callEndSends.size());
        for(CallEndSend callEndSend:callEndSends){
            //异步转存文件到天翼云
            AsyncManager.me().execute(AsyncFactory.saveToCloud(callEndSend.getRecordUrl(),callEndSend.getCallid()));
        }
    }
    Date subtractMinutes(Date date,int minute){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE,minute);
        return calendar.getTime();
    }



}
